package com.lcw.lostandfound.controller.backstage;

import com.github.pagehelper.PageInfo;
import com.lcw.lostandfound.pojo.Goods;
import com.lcw.lostandfound.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AGoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/backstage/goods")
    public String goods(Model model, @RequestParam(name = "pageNum",required = false,defaultValue = "1")Integer pageNum, @RequestParam(name = "pageSize",required = false,defaultValue = "10")Integer pageSize){
        model.addAttribute("goods",goodsService.listGoods(pageNum,pageSize));
        return "admin/goods";
    }
    @ResponseBody
    @RequestMapping("/deleteGoods")
    public String deleteGoods(Integer id){
        if (goodsService.delete(id)>0) {
            return "true";
        }
        return "false";
    }
    @RequestMapping("/searchGoods")
    public String search(Model model,@RequestParam(name = "pageNum",required = false,defaultValue = "1")Integer pageNum,@RequestParam(name = "pageSize",required = false,defaultValue = "10")Integer pageSize,@RequestParam(name = "key",required = false)String key){
        PageInfo<Goods> pages = goodsService.findByLike(pageNum, pageSize, key);
        if (pages.getPages()==0){
            pages.setPages(1);
        }
        model.addAttribute("goods",pages);
        model.addAttribute("search",key);
        System.out.println(pages.getPages());
        return "admin/Findgoods";
    }
}
